package com.aierjun.tencentmaplib;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;

import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdate;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.SupportMapFragment;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.UiSettings;
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptor;
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory;
import com.tencent.tencentmap.mapsdk.maps.model.CameraPosition;
import com.tencent.tencentmap.mapsdk.maps.model.CircleOptions;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;

public class MapLocationActivity2 extends FragmentActivity implements TencentLocationListener {

    private TencentMap tencentMap;
    private TencentLocationManager locationManager;
    private TencentLocationRequest locationRequest;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.activity_map_location);
        init();
//        showLocationSetting();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        //这里调用了腾讯定位sdk,如果不再需要定位功能，必须关闭定位。
        if (tencentMap.isMyLocationEnabled()) {
            tencentMap.setMyLocationEnabled(false);
        }
    }

    protected void init() {
        locationManager = TencentLocationManager.getInstance(this);
        locationRequest = TencentLocationRequest.create();
        locationRequest.setInterval(2000);
        locationRequest.setAllowDirection(true);

        locationManager.requestLocationUpdates(locationRequest, this);

        FragmentManager fm = getSupportFragmentManager();
        SupportMapFragment mapFragment =
                (SupportMapFragment) fm.findFragmentById(R.id.frag_map);
        tencentMap = mapFragment.getMap();
        UiSettings uiSettings = tencentMap.getUiSettings();
        uiSettings.setZoomControlsEnabled(false);
        uiSettings.setCompassEnabled(true);
        uiSettings.setMyLocationButtonEnabled(true);

    }

    protected void showLocationSetting() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            builder.setMessage("GPS is enabled, do you want to change it?");
        } else {
            builder.setMessage("GPS is disabled, do you want to change it?");
        }
        builder.setPositiveButton("Yes", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("No", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        builder.create().show();
    }


    private void compassF() {
        if (compass)
            compass = false;
        else
            compass = true;
        if (compass) {
            Log.d("Ani", "onClick() 1111");
            CameraUpdate cameraSigma =
                    CameraUpdateFactory.newCameraPosition(new CameraPosition(
                            new LatLng(latitude, longitude), //新的中心点坐标
                            15,  //新的缩放级别
                            0f, //俯仰角 0~45° (垂直地图时为0)
                            45f)); //偏航角 0~360° (正北方为0)
            tencentMap.moveCamera(cameraSigma);
        } else {
            Log.d("Ani", "onClick() 2222");
            CameraUpdate cameraSigma =
                    CameraUpdateFactory.newCameraPosition(new CameraPosition(
                            new LatLng(latitude, longitude), //新的中心点坐标
                            15,  //新的缩放级别
                            45f, //俯仰角 0~45° (垂直地图时为0)
                            45f)); //偏航角 0~360° (正北方为0)
            tencentMap.moveCamera(cameraSigma);
        }
    }

    private boolean move = true;
    private boolean compass = true;
    private double latitude = 0;
    private double longitude = 0;

    @Override
    public void onLocationChanged(TencentLocation tencentLocation, int i, String s) {
        if (i == TencentLocation.ERROR_OK) {
            Log.e("maplocation", "location: " + tencentLocation.getCity() + " " + tencentLocation.getProvider());
            Location location = new Location(tencentLocation.getProvider());
            latitude = tencentLocation.getLatitude();
            longitude = tencentLocation.getLongitude();
            location.setLatitude(tencentLocation.getLatitude());
            location.setLongitude(tencentLocation.getLongitude());
            location.setAccuracy(tencentLocation.getAccuracy());
            Log.d("Ani rotate",(int)tencentLocation.getExtra().getDouble("direction") +"");
            BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.red_location);
            if (move) {
                tencentMap.addMarker(new MarkerOptions(new LatLng(tencentLocation.getLatitude(), tencentLocation.getLongitude()))
                .icon(bitmapDescriptor)
                .title("我的位置"));
                tencentMap.addCircle(new CircleOptions().
                        center(new LatLng(tencentLocation.getLatitude(), tencentLocation.getLongitude())).
                        radius(100d).
                        fillColor(0x390000ff).
                        strokeColor(0x990000ff).
                        strokeWidth(1));
                CameraUpdate cameraSigma =
                        CameraUpdateFactory.newCameraPosition(new CameraPosition(
                                new LatLng(tencentLocation.getLatitude(), tencentLocation.getLongitude()), //新的中心点坐标
                                17,  //新的缩放级别
                                0f, //俯仰角 0~45° (垂直地图时为0)
                                45f)); //偏航角 0~360° (正北方为0)
                tencentMap.moveCamera(cameraSigma);
            }
            move = false;

            BitmapUtils.rotate(bitmapDescriptor.getBitmap(this),(int)tencentLocation.getExtra().getDouble(TencentLocation.EXTRA_DIRECTION),true);
        }
    }

    @Override
    public void onStatusUpdate(String s, int i, String s1) {

    }
}